<?php

namespace App\Admin\Controllers;

use App\Admin\Extensions\Actions\GoodsImport;
use App\Admin\Extensions\Actions\GoodsStatus;
use App\Helpers\Helpers;
use App\Model\Category;
use App\Model\Goods;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Show;
use Encore\Admin\Widgets\Box;
use Illuminate\Validation\Rule;

class GoodsController extends AdminController
{
    /**
     * Title for current resource.
     *
     * @var string
     */
    protected $title = '商品列表';

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new Goods);

        $grid->column('id', __('Id'));
        $grid->column('category_id', __('Category id'))->using(Helpers::getModelSelect(Category::get(), 'name'));
        $grid->column('goods_name', __('Goods name'));
        $grid->column('goods_thumb', __('Goods thumb'))->image('/uploads', 200);
        $grid->column('thumb_url', '网络图片')->image(null, 200);
//        $grid->column('goods_thumb', __('Goods thumb'))->display()->image('/uploads', 200)->dis;
        $grid->column('goods_price', __('Goods price'));
        $grid->column('sale_num', __('Sale num'));
        $grid->column('created_at', __('Created at'));

        $grid->column('status', __("Status"))->action(GoodsStatus::class);

        $grid->tools(function (Grid\Tools $tools) {
            $tools->append(new GoodsImport());
        });

        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(Goods::findOrFail($id));

        $show->field('id', __('Id'));
        $show->field('category_id', __('Category id'));
        $show->field('goods_name', __('Goods name'));
        $show->field('goods_thumb_id', __('Goods thumb id'));
        $show->field('goods_thumb', __('Goods thumb'));
        $show->field('goods_price', __('Goods price'));
        $show->field('sale_num', __('Sale num'));
        $show->field('detail', __('Detail'));
        $show->field('deleted_at', __('Deleted at'));
        $show->field('created_at', __('Created at'));
        $show->field('updated_at', __('Updated at'));

        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new Goods);


        $form->text('goods_name', __('Goods name'))
            ->creationRules([
                'required',
//                Rule::unique('goods', 'goods_name')->whereNull('deleted_at')
            ])
            ->updateRules([
                'required',
//                Rule::unique('goods', 'goods_name')->whereNull('deleted_at')->ignore($form->model()->id)
            ])
            ->required();
        $form->select('category_id', __('Category id'))
            ->rules('required')
            ->required()
            ->options(Helpers::getModelSelect(Category::get(), 'name'));
        $form->decimal('goods_price', __('Goods price'))
            ->default(0.00)->required();
        $form->image('goods_thumb', __('Goods thumb'))->uniqueName();

        $form->url('thumb_url', '网络图片')->rules('url');

        $form->text('out_url', __('Out Url'))
            ->rules('url')
            ->required();

        $form->editor('detail', __('Detail'))->required();

        $form->saving(function (Form $form) {
            $form->model()->status = Goods::STATUS_ONLINE;
        });


        return $form;
    }
}
